package com.nch.haitonghop.others;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.preference.PreferenceManager;

/**
 * Created by nguyenconghuy on 1/3/15.
 */

public class RateRequestManager {

    private static RateRequestManager instance = null;
    private static int kCountToAskRateDefault = 15;
    private static String kRateKey = "rateKey";

    public static int kRateStatusZero = 0;
    public static int kRateStatusRated = -1;
    public static int kRateStatusDontAskAgain = -2;

    Context context;

    int countToAskRate;

    // Example: https://play.google.com/store/apps/details?id=com.nch.smsbackupandrestore
    String appRateUrlString;

    private RateRequestManager() {
        countToAskRate = kCountToAskRateDefault;
    }

    public static RateRequestManager getInstance() {
        if(instance == null) {
            instance = new RateRequestManager();
        }
        return instance;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public void setCountToAskRate(int countToAskRate) {
        this.countToAskRate = countToAskRate;
    }

    public void setAppRateUrlString(String appRateUrlString) {
        this.appRateUrlString = appRateUrlString;
    }

    int getRateStatus() {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        return preferences.getInt(kRateKey, kRateStatusZero);
    }

    void setRateStatus(int rateStatus) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt(kRateKey, rateStatus);
        editor.commit();
    }

    public void updateAndShowRateRequestIfNeed() {
        int rateStatus = getRateStatus();
//        Log.d("", "Rate status: " + rateStatus);

        if((rateStatus != kRateStatusDontAskAgain && rateStatus != kRateStatusRated) && rateStatus >= countToAskRate) {
            showDialog();
        }

        if(rateStatus >= kRateStatusZero) {
            rateStatus++;
            setRateStatus(rateStatus);
        }
    }

    void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setTitle("Đánh giá ứng dụng")
                .setMessage("Vui lòng cho chúng tôi biết đánh giá của bạn, để chúng tôi có thể cải thiện ứng dụng tốt hơn")

                .setPositiveButton("Đánh giá", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        rateNow();
                    }
                })

                .setNeutralButton("Nhắc tôi sau", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        remindMeLater();
                    }
                })

                .setNegativeButton("Đừng nhắc lại", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dontRemindAgain();
                    }
                });
        builder.show();
    }

    void rateNow() {
        setRateStatus(kRateStatusRated);

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(appRateUrlString));
        context.startActivity(intent);
    }

    void remindMeLater() {
        setRateStatus(kRateStatusZero);
    }

    void dontRemindAgain() {
        setRateStatus(kRateStatusDontAskAgain);
    }
}
